<html>
    <head>
        <script type="text/javascript" src="..\Core\jquery-1.2.6.js"></script>
        <script type="text/javascript" src="..\Core\jstest.js"></script>
        
    </head>
    <body>
        <h4>Fixture tests</h4>
        
        <script type="text/javascript">
            $(document).ready( function() {

            function assert(expr, msg) {
                if (expr)
                    console.log("ok: " + assert.caller.name);
                else
                    console.error("FAILED: " + assert.caller.name);
            }


            function assertEqual(expected, actual) {
                if (expected === actual)
                    console.log("ok: " + assertEqual.caller.name);
                else
                    console.error("FAILED: " + assertEqual.caller.name + " -> expected: " + expected + ", actual: " + actual);
            }

            function createSuite() {
                return new SpecSuite().FindContextsStartingWith("context");
            }


            (function can_create_fixture_from_fixture_object() {
                var fixture = new SpecFixture({});
                
                assertEqual(0, fixture.contextMethods.length);
                assertEqual(0, fixture.testMethods.length);            
            })();

            (function fixture_identifies_context_and_test_methods() {
                var fixtureObj = {
                    context: function() {},
                    "should find first test": function() {},
                    "should find next test": function() {}
                }
                var fixture = new SpecFixture("test fixture", fixtureObj);
                
                assertEqual(1, fixture.contextMethods.length);
                assertEqual(2, fixture.testMethods.length);
            })();
            
            (function fixture_executes_context_first() {
                var contextRun = false;                
                var contextRunFirst = false;
                
                var fixtureObj = {
                    context: function() { contextRun = true; },
                    "should first test": function() { contextRunFirst = contextRun; },
                    "should next test":  function() { contextRunFirst = contextRun; }
                }
                var outputHandler = {
                    logFixtureName: function(name) {
                    },
                };
                var fixture = new SpecFixture("test fixture", fixtureObj);
                fixture.execute(outputHandler);
                assert(contextRunFirst);
            })();
            
            (function failing_tests_call_correct_output_handler_method() {                
                var fixtureObj = {
                    context: function() { },
                    "should first test": function() { throw { message: "test error"}; },
                    "should next test":  function() { }
                }
                
                var failedTestName = "";
                var outputHandler = {
                    logFixtureName: function(name) {
                    },
                    logFailedTest: function(testName, err) {
                      failedTestName = testName;
                    }
                }
                
                var fixture = new SpecFixture("test fixture", fixtureObj);
                fixture.execute(outputHandler);
                
                assertEqual("should first test", failedTestName);
            })();

            (function failing_context_is_logged_and_prevents_tests_from_running() {                
                var fixtureObj = {
                    context: function() { throw { message: "context error" }; },
                    "first test": function() { throw { message: "test error"}; },
                    "next test":  function() { }                    
                }
                
                var failedTestName = "";
                var testWasRun = false;
                var outputHandler = {
                    logFixtureName: function(name) {
                    },
                    logFailedContext: function(testName, err) {                        
                        failedTestName = testName;
                    },
                    logFailedTest: function(testName, err) {
                        testWasRun = true;
                    }
                }
                
                var fixture = new SpecFixture("fixture name", fixtureObj);
                fixture.execute(outputHandler);
                
                assertEqual("context", failedTestName);
                assert(testWasRun == false);
            })();
            
            
            (function sends_fixture_name_to_output_first() {
                var fixtureObj = {
                    context: function() { throw { message: "context error" }; },
                    "first test": function() { throw { message: "test error"}; },
                    "next test":  function() { }                    
                }
                
                var fixtureNameCalled = false;
                var fixtureNameCalledFirst = false;
                
                var outputHandler = {
                    logFixtureName: function(name) {
                        fixtureNameCalled = true;
                    },
                    logFailedContext: function(testName, err) {                        
                        fixtureNameCalledFirst = fixtureNameCalled;
                    },
                    logFailedTest: function(testName, err) {                        
                    }
                }
                
                var fixture = new SpecFixture("fixture name", fixtureObj);
                fixture.execute(outputHandler);
                
                assert(fixtureNameCalledFirst);    
            })();


            (function should_have_proper_execution_context_so_this_references_fixture() {
                var correctValueFound = false;
                var fixtureObj = { 
                    someValue: 5,
                    
                    changeSomeValue: function(newValue) {
                        this.someValue = newValue;
                    },
                    
                    "should": function () { 
                        this.changeSomeValue(10);
                        correctValueFound = (this.someValue == 10); 
                    }
                };
                
                var outputHandler = {
                    logFixtureName: function(name) {},
                    logFailedContext: function(testName, err) {},
                    logFailedTest: function(testName, err) { console.log('failed: ' + testName + ' --> ' + err); }
                }
                
                var fixture = new SpecFixture("fixture name", fixtureObj);                
                fixture.execute(outputHandler);
                
                assert(correctValueFound);
            })()
            

                /*
                TODO:
                - should handle options for defining test/context method identification                
                */


            

            });
        
        </script>
    </body>
</html>


